package leetcode.medium.numberofspecialcharsii;

import java.util.*;

public class Solution {
    public int numberOfSpecialChars(String word) {
        Map<Character, Integer[]> map = new HashMap<>();
        for (int i = 0; i < word.length(); i++) {
            char c = word.charAt(i);
            if (!map.containsKey(Character.toLowerCase(c))) {
                Integer[] list = new Integer[2];
                if (Character.isLowerCase(c)) {
                    list[0] = i;
                } else {
                    list[1] = i;
                }
                map.put(Character.toLowerCase(c), list);
            } else {
                if (Character.isLowerCase(c)) {
                    map.get(c)[0] = i;
                } else {
                    if (map.get(Character.toLowerCase(c))[1] == null) {
                        map.get(Character.toLowerCase(c))[1] = i;
                    }
                }
            }
        }
        return (int) map.entrySet()
                .stream()
                .filter(entry -> {
                    Integer[] indices = entry.getValue();
                    if (indices[0] == null || indices[1] == null) {
                        return false;
                    }
                    return indices[0].compareTo(indices[1]) < 0;
                })
                .count();
    }

    public static void main(String[] args) {
//        System.out.println(new Solution().numberOfSpecialChars("abc"));
//        System.out.println(new Solution().numberOfSpecialChars("aaAbcBC"));
//        System.out.println(new Solution().numberOfSpecialChars("AbBCab"));
        System.out.println(new Solution().numberOfSpecialChars("cCceDC"));
    }
}
